package com.hhc.controller;

import com.hhc.dto.ExamArrangementDTO;
import com.hhc.entity.ExamArrangementDO;
import com.hhc.query.ExamArrangementQuery;
import com.hhc.service.ExamArrangementService;
import com.hhc.utils.RestResult;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 考试安排控制层
 * </p>
 * @author pc
 */
@Slf4j
@RestController
@RequestMapping("/exam-arrangement")
@Validated
public class ExamArrangementController {

    @Autowired
    private ExamArrangementService examArrangementService;

    @PostMapping("/add")
    public ResponseEntity<RestResult> addExamArrangement(@Valid @RequestBody ExamArrangementDTO examArrangementDTO) {
        try {
            ExamArrangementDTO addedExamArrangement = examArrangementService.addExamArrangement(examArrangementDTO);
            return ResponseEntity.ok(new RestResult("000000", "调用成功", addedExamArrangement));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new RestResult("000001", e.getMessage(), null));
        }
    }

    @PutMapping("/update")
    public ResponseEntity<RestResult> updateExamArrangement(@Valid @RequestBody ExamArrangementDTO examArrangementDTO) {
        try {
            ExamArrangementDTO updatedExamArrangement = examArrangementService.updateExamArrangement(examArrangementDTO);
            return ResponseEntity.ok(new RestResult("000000", "调用成功", updatedExamArrangement));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new RestResult("000001", e.getMessage(), null));
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<RestResult> deleteExamArrangement(@PathVariable Long id) {
        try {
            examArrangementService.deleteExamArrangement(id);
            return ResponseEntity.ok(new RestResult("000000", "调用成功", null));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new RestResult("000001", e.getMessage(), null));
        }
    }

    @GetMapping("/list")
    public ResponseEntity<RestResult> listExamArrangements(@ModelAttribute ExamArrangementQuery query) {
        try {
            Page<ExamArrangementDO> examArrangementPage = examArrangementService.listExamArrangements(query);
            return ResponseEntity.ok(new RestResult("000000", "调用成功", examArrangementPage));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new RestResult("000001", e.getMessage(), null));
        }
    }

    @GetMapping("/detail/{id}")
    public ResponseEntity<RestResult> getExamArrangementById(@PathVariable Long id) {
        try {
            ExamArrangementDTO examArrangementDTO = examArrangementService.getExamArrangementById(id);
            return ResponseEntity.ok(new RestResult("000000", "调用成功", examArrangementDTO));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new RestResult("000001", e.getMessage(), null));
        }
    }
}